package com.tengroup.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tengroup.dao.ExampaperepDao;
import com.tengroup.dao.ExampaperDao;
import com.tengroup.dao.QuestionDao;
import com.tengroup.dao.RuleDao;
import com.tengroup.entity.ExampaperEntity;
import com.tengroup.entity.ExampaperepEntity;
import com.tengroup.entity.Question;
import com.tengroup.entity.RuleEntity;
import com.tengroup.vo.Answervo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import com.tengroup.service.ExampaperepService;
@Service("examservice")
public class ExamServicempl extends ServiceImpl<ExampaperepDao,ExampaperepEntity>implements ExamService{
    @Autowired
    private ExampaperepDao exampaperepDao;
    @Autowired
    private RuleDao ruleDao;
    @Autowired
    private ExampaperDao exampaperDao;
    @Autowired
    private QuestionDao questionDao;
    @Autowired
    private QServicempl qservice;
    @Autowired
    private ExampaperService exampaperservice;
    @Qualifier("exampaperepService")
    private ExampaperepService exampaperepService;

    @Override
    public List<Question> selectQuestionsByRule(RuleEntity rule) {
        List<Question> l1 =questionDao.findbyconditions("multiple_choice","easy",rule.getCourseId(),rule.getEasyxuanze());
        List<Question> l2 =questionDao.findbyconditions("multiple_choice","medium",rule.getCourseId(),rule.getMediumxuanze());
        List<Question> l3 =questionDao.findbyconditions("multiple_choice","hard",rule.getCourseId(),rule.getHardxuanze());
        List<Question> l4 =questionDao.findbyconditions("true_false","easy",rule.getCourseId(),rule.getEasypanduan());
        List<Question> l5 =questionDao.findbyconditions("true_false","medium",rule.getCourseId(),rule.getMediumpanduan());
        List<Question> l6 =questionDao.findbyconditions("true_false","hard",rule.getCourseId(),rule.getHardpanduan());
        List<Question> all=new ArrayList<>();
        all.addAll(l1);
        all.addAll(l2);
        all.addAll(l3);
        all.addAll(l4);
        all.addAll(l5);
        all.addAll(l6);
        return all;
    }


    

    @Override
    public List<Question> findbyconditions(String question_type, String difficulty,Integer subject_id,int limit) {
        return questionDao.findbyconditions(question_type, difficulty, subject_id,limit);
    }

    @Override
    public void save(RuleEntity rule) {
        List<Question> questions = selectQuestionsByRule(rule);

        ExampaperEntity examPaperEntity = new ExampaperEntity();
        examPaperEntity.setRuleid(Math.toIntExact(rule.getId()));
        examPaperEntity.setRulename(rule.getName());
        examPaperEntity.setCreatetime(LocalDateTime.now());
        examPaperEntity.setQuestioncount(rule.getQuestionCount());
        exampaperDao.insert(examPaperEntity);
        for (Question question : questions){
            ExampaperepEntity examPaperepEntity = new ExampaperepEntity();
            examPaperepEntity.setQuestionid(Math.toIntExact(question.getId()));
            examPaperepEntity.setRuleid(Math.toIntExact(rule.getId()));
            examPaperepEntity.setContent(question.getStem());
            examPaperepEntity.setOptions(question.getOptions());
            examPaperepEntity.setQuestiontype(question.getQuestionType());
            examPaperepEntity.setDifficulty(question.getDifficulty());
            examPaperepEntity.setExampaperid(examPaperEntity.getId());
            exampaperepDao.insert(examPaperepEntity);
        }
    }

    @Override
    public ExampaperEntity setanswer(Answervo answervo) {
        List<ExampaperepEntity> questions=exampaperepDao.findbyexampaperid(answervo.getExampaperid());
        int i;
        ExampaperEntity examPaperEntity = exampaperservice.getById(answervo.getExampaperid());
        examPaperEntity.setName(answervo.name);
        if(answervo.method==0){
        i=0;
        for(ExampaperepEntity examPaperepEntity:questions){
            examPaperepEntity.setZuoda(answervo.getAnswer(i));
            baseMapper.updateById(examPaperepEntity);
            i=i+1;
        }
        }
        else{
            i=answervo.questionid;
            ExampaperepEntity e=questions.get(i-1);
            e.setZuoda(answervo.getAnswer(0));
            baseMapper.updateById(e);
        }
        return examPaperEntity;
    }

    @Override
    public List<ExampaperepEntity> findbyexampaperid() {
        return exampaperepDao.findbyexampaperid(1);
    }

    @Override
    public ExampaperEntity judge(int i){
        List<ExampaperepEntity> exampaperepEntities = exampaperepDao.findbyexampaperid(i);
        int sum=0;
        ExampaperEntity s=exampaperservice.getById(i);
        for(ExampaperepEntity exampaperepEntity:exampaperepEntities){
            Question q=qservice.getById(exampaperepEntity.getQuestionid());
            if(q.getAnswer().equals(exampaperepEntity.getZuoda())){
                sum+=2;
                exampaperepEntity.setJudge(1);
            }
            else{
                exampaperepEntity.setJudge(0);
            }
            exampaperepEntity.setAnswerby(s.getName());
            baseMapper.updateById(exampaperepEntity);
        }
        s.setGrade(sum);
        return s;
    }
}


